package com.abc.live.widget.common;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abc.live.ImageBean;
import com.abc.live.OrderBean;
import com.abc.live.R;
import com.abc.live.util.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.liveaa.livemeeting.sdk.util.BitmapUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.autosize.internal.CancelAdapt;

/**
 * Created by Administrator on 2017/8/25.
 * 选择分组
 */

public class ShowQuestionsDialog extends Dialog implements CancelAdapt {


    private Activity mContext;
    private OnImgClickListener listener;
    private OrderBean data;

    public ShowQuestionsDialog(Activity context) {
        super(context, R.style.abc_class_dialog);
        this.mContext = context;
    }

    public ShowQuestionsDialog(Activity context, OnImgClickListener listener, OrderBean data) {
        super(context, R.style.abc_class_dialog);
        this.mContext = context;
        this.data = data;
        this.listener = listener;
    }

    protected ShowQuestionsDialog(Activity context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_question, null);
        setContentView(view);
        //设置dialog大小
        Window dialogWindow = getWindow();
//        dialogWindow.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        WindowManager manager = ((Activity) mContext).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.7);
        params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(params);
        setCanceledOnTouchOutside(false);

        initView();

    }

    private void initView() {
        imageList.clear();
        TextView contentTv = findViewById(R.id.contentTv);
        RecyclerView imgRecyclerView = findViewById(R.id.imgRecyclerView);
        TextView txt_cancel = findViewById(R.id.txt_cancel);
        if (data != null) {
            String questionIntroduction = data.getQuestionIntroduction();
            String questionImages = data.getQuestionImages();
            if (TextUtils.isEmpty(questionIntroduction)) {
                contentTv.setVisibility(View.GONE);
            } else {
                contentTv.setVisibility(View.VISIBLE);
                contentTv.setText(questionIntroduction);
            }

            if (TextUtils.isEmpty(questionImages)) {
                imgRecyclerView.setVisibility(View.GONE);
            } else {
                imgRecyclerView.setVisibility(View.VISIBLE);
                final List<String> imgList = new ArrayList<>();
                String[] imgs = data.getQuestionImages().split(",");
                for (int i = 0; i < imgs.length; i++) {
                    imgList.add(imgs[i]);
                }
                LinearLayoutManager imgLayoutManager = new LinearLayoutManager(getContext());
                imgLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                imgRecyclerView.setLayoutManager(imgLayoutManager);
                imgRecyclerView.setAdapter(new CommonAdapter<String>(getContext(), R.layout.item_add_problem_img_new, imgList) {
                    @Override
                    protected void convert(ViewHolder holder, final String s, final int position) {
                        ImageView imgIv = holder.getView(R.id.imgIv);
                        loadCornerImageWithDefault(getContext(), s, R.drawable.default_img, 4, imgIv);
                        imgIv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                for (ImageBean bean : imageList) {
                                    if (bean.url.equals(s)) {
                                        listener.onClick(ShowQuestionsDialog.this, bean);
                                    }
                                }
//                                ABCLiveSDK.showToast(s);
                                dismiss();
                            }
                        });
                    }
                });
            }
        }


        txt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

    }


//    public  int[] getImageWidthHeight(String url){
//
//        Glide.with(getContext())
//                .load(url)
//                .into(new SimpleTarget<Drawable>() {
//                    @Override
//                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
//                        int width = resource.getIntrinsicWidth();
//                        int height = resource.getIntrinsicHeight();
//
//                    }
//                });
//    }

    List<ImageBean> imageList = new ArrayList<>();

    public void loadCornerImageWithDefault(Context context, final String obj, int defaultResId, int corner, final ImageView imageView) {

        RequestOptions circleOptions = new RequestOptions()
                .centerCrop()
                .placeholder(defaultResId)
                .error(defaultResId)
                .priority(Priority.HIGH)
                .transform(new GlideRoundTransform(context, corner));

        Glide.with(context)
                .load(obj)
                .apply(circleOptions)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        int width = resource.getIntrinsicWidth();
                        int height = resource.getIntrinsicHeight();
                        //计算压缩比例
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.outWidth = width;
                        options.outHeight = height;
                        int small = width > height ? height : width;
                        if (small > 1024) {
                            options.inSampleSize = BitmapUtil.computeSampleSize(options, 1024, 1048576L);
                        } else {
                            options.inSampleSize = 1;
                        }
                        int newWidth = (int) width / options.inSampleSize;
                        int newHeight = (int) height / options.inSampleSize;
                        ImageBean imageBean = new ImageBean(obj, newWidth, newHeight);
                        imageList.add(imageBean);
                        imageView.setImageDrawable(resource);
                    }
                });
    }


    public interface OnImgClickListener {
        void onClick(Dialog dialog, ImageBean imageBean);
    }
}
